<?php

/**
 * @author krku
 */
function smarty_function_formField($params, &$smarty) {

	$errorList = $smarty->get_template_vars('errorList');
	$alterValue = '';

	if (!isset($params['errorClass'])) {
		$params['errorClass'] = "warning";
	}

	if (isset($params['alterValue'])) {
		$alterValue = $params['alterValue'];
	}

	if (isset($params['value']) && $params['type'] != 'checkbox' && $params['type'] != 'radio' || $params['checked'] == true) {
		$value = $params['value'];
	} else if (Request::GetPost($params['name']) && !isset($params['noRequest'])) {
		$value = Request::GetPost($params['name']);
	} else if (isset($params['obj']) && $smarty->get_template_vars($params['obj']) && is_object($smarty->get_template_vars($params['obj'])) && isset($params['func'])) {
		$object = $smarty->get_template_vars($params['obj']);
		$func = $params['func'];
		$value = $object->$params['func']();
	} else {
		$value = $alterValue;
	}

	unset($params['checked']);

	if ($params['type'] != "textarea") {
		$value = Utils::SaveInput($value);
	}

	if (isset($params['obj'])) {
		unset($params['obj']);
	}

	if (isset($params['func'])) {
		unset($params['func']);
	}

	if (isset($params['noError'])) {
		$noError = $params['noError'];
		unset($params['noError']);
	}

	if (!$value && $value !== "0") {
		$value = '';
	}

	if (!isset($params['type'])) {
		trigger_error('Parametr `type` jest wymagany.');
	}

	if (!isset($params['name'])) {
		trigger_error('Parametr `name` jest wymagany.');
	}

	$tagParameters = '';
	foreach ($params as $tag => $val) {
		if ($tag != "errorClass" && $tag != 'options' && $tag != 'opt_from' && $tag != 'opt_to' && $tag != 'value')  {
			$tagParameters[$tag] = $val;
		}
	}

	if ($params['type'] == 'select') {
		$options = array();

		if (isset($params['options'])) {
			foreach ($params['options'] as $k => $v) {
				if (is_array($v)) {
					$options[] = $v;
				} else {
					$options[] = array('desc' => $v, 'value' => $k);
				}
			}
		} else if (isset($params['opt_from'])) {
			for ($i = $params['opt_from'] ; $i <= $params['opt_to'] ; $i++) {
				$options[$i] = array('value' => $i, 'desc' => $i);
			}
		} else {
			foreach ($params as $key => $val) {
				if (preg_match("/opt_(?P<key>[0-9]*)_(?P<type>.*)/", $key, $matches)) {
					if (!is_array($options[$matches['key']])) {
						$options[$matches['key']] = array();
					}

					$options[$matches['key']][$matches['type']] = $val;
				}
			}
		}

		$ret = '<select ' . __implodeTagParameters($tagParameters) . '>';

		foreach ($options as $opt) {
			$ret .= '<option value="' . $opt['value'] . '"' . ($opt['value'] == $value ? ' selected="selected"' : '') . '>' . $opt['desc'] . '</option>';
		}

		$ret .= '</select>';
	} else if ($params['type'] == 'textarea') {

		$ret = '<textarea ' . __implodeTagParameters($tagParameters) . '>' . $value . '</textarea>';
	} else if ($params['type'] == 'radio' || $params['type'] == 'checkbox') {
		unset($tagParameters['checked']);

		$ret = '<input ' . __implodeTagParameters($tagParameters) . '" value="' . $params['value'] . '" ' . ($value == $params['value'] ? 'checked="checked" ' : '') . ' />';
	} else if ($params['type'] == 'image_adm' || $params['type'] == 'file_adm') {
		$tagParameters['type'] = 'file';

		if (isset($tagParameters['pathStatic'])) {
			$pathStatic = $tagParameters['pathStatic'];
			unset($tagParameters['pathStatic']);
		} else if ($smarty->get_template_vars('pathStatic')) {
			$pathStatic = $smarty->get_template_vars('pathStatic');
		} else {
			trigger_error("Nie podano ścieżki statycznej");
		}

		if (isset($tagParameters['noDelete'])) {
			unset($tagParameters['noDelete']);
		}

		$pathStatic = str_replace('//', '/', $pathStatic);

		$exp = explode('Static/upload/', $pathStatic);
		$localization = 'upload/' . $exp[1] . $value;


		if (isset($object)) {

			$pathFun = $func . 'Path';
			$ret = ($params['type'] == 'image_adm' ? '
					<table style="width: 100%;" class="pictures">
						<tbody>
							<tr>
								<td>
									<img src="' . $object->$pathFun() . '" alt="defpic" /><br>
									' . ((!isset($params['noDelete']) || !$params['noDelete']) ? '
									<input type="checkbox" id="delete_' . $tagParameters['name'] . '_' . $object->GetId() . '" name="delete_' . $tagParameters['name'] . '_' . $object->GetId() . '" /> <label for="delete_' . $tagParameters['name'] . '_' . $object->GetId() . '">usuń zdjęcie</a>
									' : '') . '
								<td>' : '') . '
								<table>
									<tbody>
										<tr>
											<td>waga:</td>
											<td><strong>' . Utils::FileProperties('filesize', $value, $pathStatic) . '</strong></td>
										</tr>' .
										($params['type'] == 'image_adm' ? '
										<tr>
											<td>wymiary:</td>
											<td><strong>' . Utils::FileProperties('dimensions', $value, $pathStatic) . '</strong></td>
										</tr>' : '') . '
										<tr>
											<td>data modyfikacji:</td>
											<td><strong>' . Utils::FileProperties('mod_time', $value, $pathStatic) . '</strong></td>
										</tr>
										<tr>
											<td>lokalizacja:</td>
											<td><strong>' . $localization . '</strong></td>
										</tr>
										<tr>
											<td>Wgraj:</td>
											<td>
												<input ' . __implodeTagParameters($tagParameters) . ' value="' . $value . '" />' .
												(isset($params['allowedTypes']) ? '<p>dozwolone typy:' . $params['allowedTypes'] . '</p>' : '') . '</td>
										</tr>
									</tbody>
								</table>' . ($params['type'] == 'image_adm' ? '
							</td>
						</tr>
					</tbody>
				</table>' : '');
		} else {
			$ret = '<input ' . __implodeTagParameters($tagParameters) . ' value="' . $value . '" />';
		}
	} else {
		$ret = '<input ' . __implodeTagParameters($tagParameters) . ' value="' . $value . '" />';
	}

	if (isset($params['info'])) {
		$ret .= '<span class="formInfo">' . $params['info'] . '</span>';
	}

	$err = '';

	if (!isset($noError) || $noError == false) {
		if (sizeof($errorList)) {
			foreach ($errorList as $k => $v) {
				if ($v['field'] == $params['name']) {
					$err .= $v['msg'];
				}
			}
		}

		if ($err) {
			$ret .= '<p ' . (isset($params['errorClass']) ? 'class="' . $params['errorClass'] . '" ' : '') . 'id="' . $params['name'] . 'Error">';
			$ret .= $err . '</p>';
		}
	}

	return $ret;
}

function __implodeTagParameters(Array $params) {
	$ret = '';

	foreach ($params as $key => $value) {
		$ret .= " $key=\"$value\"";
	}

	return $ret;
}

?>
